﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.Enums;
using Sector7.Framework.Core;
using Sector7.Medical.Events;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// PageControl.xaml 的交互逻辑
    /// </summary>
    public partial class PageControl : AddModeControlBase
    {
        #region Accessor
        public event TurnedPageEventHandler TurnedPage = null;
        #endregion

        #region Public Fucntions
        /// <summary>
        /// 初始化分页控件
        /// </summary>
        /// <param name="pageCount">总页数</param>
        public void InitializePageControl(int pageCount)
        {
            m_pageCount = pageCount;
            m_pageIndex = 0;

            UpdatePageInformation();
            UpdateActionButtonStatus();
        }

        /// <summary>
        /// 重置分页控件
        /// </summary>
        public void ResetPageCoontrol()
        {
            m_pageCount = 0;
            m_pageIndex = -1;

            UpdatePageInformation();
            UpdateActionButtonStatus();
        }

        /// <summary>
        /// 追加新页
        /// </summary>
        /// <returns></returns>
        public int AppendNewPage()
        {
            m_pageCount += 1;
            m_pageIndex = m_pageCount;

            UpdatePageInformation();
            UpdateActionButtonStatus();

            return m_pageIndex;
        }

        /// <summary>
        /// 删除当前页
        /// </summary>
        /// <returns></returns>
        public int DeleteCurrentPage()
        {
            int newPageIndex = 0;
            int newPageCount = 0;

            if (m_pageIndex >= 0 && m_pageIndex < m_pageCount - 1)
            {
                newPageIndex = m_pageIndex - 1;
                newPageCount = m_pageCount - 1;
                if (newPageCount > 0)
                {
                    if (newPageIndex < 0)
                    {
                        m_pageIndex = 0;
                    }
                    else
                    {
                        m_pageIndex = newPageIndex;
                    }
                }
                else
                {
                    ResetPageCoontrol();
                }
            }
            else
            {
                ResetPageCoontrol();
            }

            UpdatePageInformation();
            UpdateActionButtonStatus();

            return m_pageIndex;
        }
        #endregion 

        #region Private Functions
        /// <summary>
        /// 更新当前分页信息
        /// </summary>
        private void UpdatePageInformation()
        {
            string pageContent = string.Format("[ {0} / {1} ]", m_pageIndex, m_pageCount);
            lblPage.Content = pageContent;
        }

        /// <summary>
        /// 更新分页动作按钮状态
        /// </summary>
        private void UpdateActionButtonStatus()
        {
            int pageNo = m_pageIndex;
            if (m_pageCount > 0)
            {
                if (pageNo == 1)
                {
                    btnPrevious.IsEnabled = false;
                }
                else
                {
                    btnPrevious.IsEnabled = true;
                }
                
                if (pageNo == m_pageCount)
                {
                    btnNext.IsEnabled = false;
                }
                else
                {
                    btnNext.IsEnabled = true;
                }
            }
            else
            {
                btnPrevious.IsEnabled = false;
                btnNext.IsEnabled = false;
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="turnPageDirection">翻页方向</param>
        /// <param name="turnPageCount">翻页数量</param>
        /// <returns></returns>
        private bool TurnPage(TurnPageDirectionType turnPageDirection, int turnPageCount)
        {
            bool ret = false;

            switch(turnPageDirection)
            {
                case TurnPageDirectionType.Previous:
                    if (turnPageCount < 0)
                    {
                        ret = false;
                    }
                    else
                    {
                        ret = true;
                    }
                    break;
                case TurnPageDirectionType.Next:
                    if (turnPageCount > m_pageCount)
                    {
                        ret = false;
                    }
                    else
                    {
                        ret = true;
                    }
                    break;
                default:
                    break;
            }

            if (ret)
            {
                m_pageIndex = turnPageCount;
                UpdatePageInformation();
                UpdateActionButtonStatus();
            }

            return ret;
        }

        /// <summary>
        /// 跳转到指定页
        /// </summary>
        /// <param name="pageNo">跳转到的页码</param>
        /// <returns></returns>
        private bool GotoPage(int pageNo)
        {
            bool ret = false;

            if (pageNo < 1 || pageNo > m_pageCount)
            {
                ret = false;
                m_errors.Clear();
                m_errors.Add("翻页失败", "发生内部计算错误!");
                ShowErrors();
            }
            else
            {
                ret = true;
                m_pageIndex = pageNo;
                UpdatePageInformation();
                UpdateActionButtonStatus();
            }

            return ret;
        }

        protected void OnTurnPage(TurnedPageEventAgrs e)
        {
            if (TurnedPage != null)
            {
                TurnedPage(this, e);
            }
        }

        /// <summary>
        /// 点击上一页
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void btnPrevious_Click(object obj, RoutedEventArgs e)
        {
            int pageNo = m_pageIndex - 1;
            bool ret = TurnPage(TurnPageDirectionType.Previous, pageNo);
            if (!ret)
            {
                m_errors.Clear();
                m_errors.Add("翻页失败", "发生内部计算错误!");
                ShowErrors();
            }
            else
            {
                OnTurnPage(new TurnedPageEventAgrs(pageNo, TurnPageDirectionType.Previous));
            }
        }

        /// <summary>
        /// 点击下一页
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object obj, RoutedEventArgs e)
        {
            int pageNo = m_pageIndex + 1;
            bool ret = TurnPage(TurnPageDirectionType.Next, pageNo);

            if (!ret)
            {
                m_errors.Clear();
                m_errors.Add("翻页失败", "发生内部计算错误!");
                ShowErrors();
            }
            else
            {
                OnTurnPage(new TurnedPageEventAgrs(pageNo, TurnPageDirectionType.Next));
            }
        }

        //组合键Crtl+G
        private void btnComix_Click(object obj, RoutedEventArgs e)
        { 
        
        }
        #endregion

        #region Structrue
        public PageControl()
        {
            InitializeComponent();
            InitializePageControl(0);
        }
        #endregion

        #region Inner Member
        /// <summary>
        /// 获取当前页码
        /// </summary>
        private int m_pageIndex = 0;

        /// <summary>
        /// 获取总页数
        /// </summary>
        private int m_pageCount = 0;
        #endregion
    }
}
